﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace CDMS.CommonForm
{
   public class SkinManagerEx
    {
       public SkinManagerEx()
       {
           //RegisterAssembly for skin
           DevExpress.Skins.SkinManager.Default.RegisterAssembly(typeof(DevExpress.UserSkins.BonusSkins).Assembly);
           DevExpress.Skins.SkinManager.Default.RegisterAssembly(typeof(DevExpress.UserSkins.OfficeSkins).Assembly);
           DevExpress.Skins.SkinManager.EnableFormSkins();
       }
       public void InitSkinGallery(DevExpress.XtraBars.RibbonGalleryBarItem ribbonGalleryBarItem)
       {
           DevExpress.XtraBars.Helpers.SkinHelper.InitSkinGallery(ribbonGalleryBarItem, true);

           //Get skin name default
           DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle("Default");

           //Get skin name from app.config
           DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(ConfigurationManager.AppSettings["skinname"].ToString());
           ribbonGalleryBarItem.GalleryItemClick += ribbonGalleryBarItem_GalleryItemClick;
       }

       void ribbonGalleryBarItem_GalleryItemClick(object sender, DevExpress.XtraBars.Ribbon.GalleryItemClickEventArgs e)
       {
           SaveSkin(e.Item.Caption);
       }

       public void SaveSkin(string skinName)
       {
           try
           {
               //Save skin after option

               DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(skinName);
               XmlDocument appDoc = new XmlDocument();
               string appConfigFile = String.Format("{0}.config", Application.ExecutablePath);
               appDoc.Load(appConfigFile);
               XmlNode node = appDoc.SelectSingleNode("/configuration/appSettings/add");
               node.Attributes["value"].Value = skinName;
               appDoc.Save(appConfigFile);
           }
           catch (Exception) { DevExpress.XtraEditors.XtraMessageBox.Show("Không lưu được thiết lập, kiểm tra lại tập tin app.config.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); }
       }

    }
}
